package com.chookss.home.gallery;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.DecelerateInterpolator;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bm.library.PhotoView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.chookss.R;
import com.chookss.tools.GlideUtils;
import com.chookss.tools.Utils;
import com.lvgroup.hospital.base.BaseAct;

import org.jetbrains.annotations.Nullable;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GalleryBigImageActivity extends BaseAct {

    @BindView(R.id.llBg)
    RelativeLayout llBg;
    @BindView(R.id.llTop)
    LinearLayout llTop;
    @BindView(R.id.statusBar)
    View statusBar;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvTime)
    TextView tvTime;
    @BindView(R.id.tvPeopleTime)
    TextView tvPeopleTime;
    @BindView(R.id.tvLocation)
    TextView tvLocation;
    @BindView(R.id.photoview)
    PhotoView photoView;
    @BindView(R.id.llBottom)
    LinearLayout llBottom;
    @BindView(R.id.llShare)
    LinearLayout llShare;
    @BindView(R.id.llDetail)
    LinearLayout llDetail;
    private String dataCode;
    private boolean isFullScreen = false;//是否全屏
    private String url = "";
    private Bitmap bitmap = null;
    private Animation animationUpIn=null,animationUpOut=null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gallery_big_image);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        initState2();
        tvTitle.setText("2022年1月11日");
        tvTime.setText("20:32");
        int statusBarHeight = Utils.getStatusBarHeight(this);
        LinearLayout.LayoutParams topAllParams = (LinearLayout.LayoutParams) statusBar.getLayoutParams();
        topAllParams.height = statusBarHeight;
        statusBar.setLayoutParams(topAllParams);
        RelativeLayout.LayoutParams photoViewParams = (RelativeLayout.LayoutParams) photoView.getLayoutParams();
        photoViewParams.setMargins(0,statusBarHeight+Utils.dip2px(this,50),0,Utils.dip2px(this,60));
        photoView.setLayoutParams(photoViewParams);
        photoView.enable();
        showLoading();
        url = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1114%2F020421140I8%2F210204140I8-5-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1655454898&t=430c7226f375dbe08ef045987670aa6a";
        GlideUtils.load(this, "", photoView, R.drawable.default_news);
        Glide.with(this).asBitmap()
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).load(url)
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@androidx.annotation.NonNull Bitmap resource, @androidx.annotation.Nullable Transition<? super Bitmap> transition) {
                        try {
                            dismissLoading();
                            if (resource != null) {
                                bitmap = resource;
                                Glide.with(GalleryBigImageActivity.this).load(bitmap).into(photoView);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
        animationUpOut = AnimationUtils.loadAnimation(this, R.anim.view_up_out);
        animationUpIn = AnimationUtils.loadAnimation(this, R.anim.view_up_in);
        photoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isFullScreen) {
                    isFullScreen = false;
                    setAnim(1);
                } else {
                    isFullScreen = true;
                    setAnim(0);
                }
            }
        });
    }

    private void setAnim(int type) {
        //1展开  0缩回只留大图  全屏
        if (type == 0) {
            fadeOut(llTop, 300, null, true);
            fadeOut(llBottom, 300, null, true);
        } else {
            fadeIn(llTop, 300, null, true);
            fadeIn(llBottom, 300, null, true);
        }
    }
    public static AlphaAnimation fadeOut(final View view, int duration, final Animation.AnimationListener listener, boolean isNeedAnimation) {
        if (view == null) {
            return null;
        }
        if (isNeedAnimation) {
            AlphaAnimation alpha = new AlphaAnimation(1, 0);
            alpha.setInterpolator(new DecelerateInterpolator());
            alpha.setDuration(duration);
            alpha.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    if (listener != null) {
                        listener.onAnimationStart(animation);
                    }
                }
                @Override
                public void onAnimationEnd(Animation animation) {
                    view.setVisibility(View.GONE);
                    if (listener != null) {
                        listener.onAnimationEnd(animation);
                    }
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                    if (listener != null) {
                        listener.onAnimationRepeat(animation);
                    }
                }
            });
            view.startAnimation(alpha);
            return alpha;
        } else {
            view.setVisibility(View.GONE);
            return null;
        }
    }
    public static AlphaAnimation fadeIn(View view, int duration, Animation.AnimationListener listener, boolean isNeedAnimation) {
        if (view == null) {
            return null;
        }
        if (isNeedAnimation) {
            view.setVisibility(View.VISIBLE);
            AlphaAnimation alpha = new AlphaAnimation(0, 1);
            alpha.setInterpolator(new DecelerateInterpolator());
            alpha.setDuration(duration);
            alpha.setFillAfter(true);
            if (listener != null) {
                alpha.setAnimationListener(listener);
            }
            view.startAnimation(alpha);
            return alpha;
        } else {
            view.setAlpha(1);
            view.setVisibility(View.VISIBLE);
            return null;
        }
    }
    @Override
    public void onBackPressed() {
        if (isFullScreen) {
            isFullScreen = false;
            setAnim(1);
        } else {
            super.onBackPressed();
        }
    }

    @OnClick(R.id.common_title_back)
    public void onClick() {
        finish();
    }

}
